package Engine.InputHandling;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import Engine.Utils.Vector2f;

/**
 * Use the three variables mouseClicked[] and mousePressed[] and mouseDragged[] for
 * mouse input handling.
 * @param mousePos Mouse position (updates when moving or dragging).
 * @param lastMousePosition Mouse position (updates when moving only).
 * @author Lueko
 *
 */
public class Mouse implements MouseListener, MouseMotionListener, MouseWheelListener {

    public static boolean[] mouseClicked = new boolean[5];
    public static boolean[] mousePressed = new boolean[5];
    public static boolean[] mouseDragged = new boolean[5];
    
    private static Vector2f mousePos = new Vector2f(0 ,0);
    private static boolean mouseWheelMoved = false;
    private static int mouseWheel;
   
    private static Vector2f lastMousePos = new Vector2f(0, 0);

	@Override
	public void mouseDragged(MouseEvent e) {
		mouseDragged[e.getButton()] = true;
		mousePos.setVector(e.getX(), e.getY());
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mousePos.setVector(e.getX(), e.getY());
		lastMousePos.setVector(mousePos);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		mouseClicked[e.getButton()] = true;
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	    mousePressed[e.getButton()] = true;
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	    mousePressed[e.getButton()] = false;
		mouseDragged[e.getButton()] = false;
	}
	
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		mouseWheelMoved = true;
		mouseWheel = e.getWheelRotation();
	}
	
	public static Vector2f getPos() {
		return mousePos;
	}
	
	public static Vector2f lastPos() {
		return lastMousePos;
	}
    
	public static int getMouseWheelRotation() {
		if(!mouseWheelMoved) {
			return 0;
		} else {
			mouseWheelMoved = false;
			return mouseWheel;
		}
	}
	
	public static boolean mouseClicked(int button) {
		boolean clicked = mouseClicked[button];
		mouseClicked[button] = false;
		return clicked;
	}
	
	public static boolean mousePressed(int button) {
		return mousePressed[button];
	}
	
	public static boolean mouseDragged(int button) {
		return mouseDragged[button];
	}
	
	public static boolean smthClicked() {
		for (int i = 0; i < mouseClicked.length; i++) {
			if (mouseClicked[i]) return true;
		}
		return false;
	}
	
	public static void resetClick() {
		for (int i = 0; i < mouseClicked.length; i++) {
			mouseClicked[i] = false;
		}
	}
}
